#include "include.h"
#include "func.h"
#include "func_music.h"
#include "func_exspiflash_music.h"
#include "api_spiflash_ex.h"

#if (EX_SPIFLASH_SUPPORT)
#define MAX_TIMEOUT_TIME 30
#define SONG_INT_NUM 5
#define POWER_ON_TTS_NUM 8

exspifalsh_msc_t exspi_msc AT(.buf.func.music);
u16 play_time=0;
u32 song_ticks = 0;
u16 delay_time=500;
AT(.text.func.music.table)
const u8 song_table[4][2]=
{
	{1,1},
	{2,2},
	{6,6},
	{4,4},
};
#if 0
AT(.text.func.music.table)
const song_time_play_t song1_time[]=
{
	{0,LED_SEQUENCE_7}, 	//1
	{101,LED_SEQUENCE_3},
	{3239,LED_SEQUENCE_4},
	{3374,LED_SEQUENCE_13},
};

AT(.text.func.music.table)
const song_time_play_t song2_time[]=
{
	{0,LED_SEQUENCE_4},		//1
	{910,LED_SEQUENCE_5},
	{4494,LED_SEQUENCE_7},
	{4651,LED_SEQUENCE_3},
	{7740,LED_SEQUENCE_7},
	{7828,LED_SEQUENCE_4},
	{9417,LED_SEQUENCE_3},
	{13295,LED_SEQUENCE_4},
	{13600,LED_SEQUENCE_7},
	{13755,LED_SEQUENCE_3},//10
	{16880,LED_SEQUENCE_5},
	{18458,LED_SEQUENCE_9},
	{18620,LED_SEQUENCE_7},
	{18772,LED_SEQUENCE_3},
	{19843,LED_SEQUENCE_12},
};
#endif

bool exspiflash_init(void)
{
    printf("exspiflash_init\n");
    spiflash1_init();
    if (!is_exspiflash_online()) {
        return false;
    }
    return true;
}
AT(.text.func.music)
void set_song_play_num(void)
{
	u8 led_mode;
	music_control(MUSIC_MSG_STOP);
	delay_time=500;
	if(exspi_msc.song_int_flag)
	{
		exspi_msc.cur_num=SONG_INT_NUM;
		led_mode=LED_SEQUENCE_4;
	}
	else if(exspi_msc.power_on_tts||exspi_msc.QR_disable_tts)
	{
		exspi_msc.cur_num=POWER_ON_TTS_NUM;
		led_mode=LED_SEQUENCE_1;
	}
	else
	{
		exspi_msc.cur_num=song_table[sys_cb.song_mode][sys_cb.project_num];
		if(sys_cb.song_mode==3)
		{
			led_mode=LED_SEQUENCE_9;			
		}
		else if(sys_cb.song_mode==2)
		{
			led_mode=LED_SEQUENCE_11;
			delay_time=300;
		}
		else if(!sys_cb.project_num)
		{
			//led_mode=song1_time[0].light_mode;
			//exspi_msc.time_offset=1;
			led_mode=LED_SEQUENCE_3;
		}
		else
		{
			//led_mode=song2_time[0].light_mode;
			//exspi_msc.time_offset=1;
			led_mode=LED_SEQUENCE_3;
		}
		song_ticks=0;
		play_time=0;
	}
	exspi_msc.pause=false;
	bsp_set_led_mode(led_mode);
	amp_speaker_unmute();	
	exspifalsh_music_num_kick(exspi_msc.cur_num);
	
}
AT(.text.func.music)
void func_check_song_play(void)
{
	if (get_music_dec_sta() == MUSIC_STOP)
	{
		if(exspi_msc.pause)
		{
			
			return;
		}
		if(exspi_msc.song_int_flag)
		{
			if(exspi_msc.song_continue_flag)
			{
				exspi_msc.song_int_flag=0;
				exspi_msc.song_continue_flag=0;
				set_song_play_num();
				return;
			}
			exspi_msc.pause=true;
			exspi_msc.timeout_cnt=0;
			exspi_msc.song_int_flag=0;
			if(sys_cb.song_mode==2&&sys_cb.bt_connected == 1)
			{
				music_control(MUSIC_MSG_STOP);
				func_cb.sta = FUNC_BT;
				return;
			}
			bsp_set_led_mode(LED_SEQUENCE_2);
			return;
		}
		else if(exspi_msc.power_on_tts)
		{
			exspi_msc.power_on_tts=0;
			set_song_play_num();
			return;
		}
		else if(exspi_msc.QR_disable_tts)
		{
			music_control(MUSIC_MSG_STOP);
			bsp_set_led_mode(LED_SEQUENCE_2);
			exspi_msc.QR_disable_tts=0;
			exspi_msc.pause=true;
			exspi_msc.timeout_cnt=0;
			return;
		}
		if(tick_check_expire(exspi_msc.delay_ticks,delay_time))
		{
			exspi_msc.delay_ticks=tick_get();
			if(!exspi_msc.ticks_start)
			{
				printf("tick delay start!\n");
				exspi_msc.ticks_start=1;
				return;
			}
		}
		else
		{
			return;
		}

		exspi_msc.ticks_start=0;
		if(sys_cb.song_mode<=1)
		{
			sys_cb.song_mode=2;
		}
		else if(sys_cb.song_mode==2)
		{
			sys_cb.song_mode=3;
		}
		else
		{
			exspi_msc.pause=true;
			exspi_msc.timeout_cnt=0;
			exspi_msc.song_int_flag=0;
			if(sys_cb.bt_connected== 1)
			{
				music_control(MUSIC_MSG_STOP);
				func_cb.sta = FUNC_BT;
				return;
			}
			bsp_set_led_mode(LED_SEQUENCE_2);
			return;
		}
		
		printf("song_mode:%d->song_int_flag:%d\n\n",sys_cb.song_mode,exspi_msc.song_int_flag);
		set_song_play_num();
	}
}
AT(.text.func.music)
void func_song_light_sync_deal(void)
{

	#if 0
	u16 song_play_time=0;
	u8 light_mode=0;
	if(exspi_msc.pause)
	{
		return;
	}
	if(exspi_msc.song_int_flag)
	{
		return ;
	}
	if(exspi_msc.power_on_tts)
	{
		return ;
	}
	if(exspi_msc.QR_disable_tts)
	{
		return ;
	}
	if(sys_cb.song_mode==2)
	{
		return;
	}	
	if(tick_check_expire(song_ticks,10))
	{
		song_ticks = tick_get();
		//printf("cur_num:%d\n",exspi_msc.cur_num);
		if(!sys_cb.project_num)
		{
			#if 0
			msc_breakpiont_t song_brkpt;
			u32 cur_time1 = music_get_cur_time();
			music_get_breakpiont(&song_brkpt);			
			printf("time:%d->frame_count:0x%x->file_ptr:0x%x\n",cur_time1,song_brkpt.frame_count,song_brkpt.file_ptr);
			#endif
			song_play_time=song1_time[exspi_msc.time_offset].time;
			light_mode=song1_time[exspi_msc.time_offset].light_mode;
		}
		else 
		{
			
			song_play_time=song2_time[exspi_msc.time_offset].time;
			light_mode=song2_time[exspi_msc.time_offset].light_mode;
			
		}
		//printf("play time:%d->offset:%d\n",play_time,exspi_msc.time_offset);
		if(play_time==song_play_time)
		{
			u32 cur_time1 = music_get_cur_time();
			if(play_time!=(cur_time1/10))
			{
				play_time=cur_time1*10;
			}
			bsp_set_led_mode(light_mode);
			
			exspi_msc.time_offset++;
		}
		play_time++;
	}
	#endif
}

AT(.text.func.music)
void func_switch_song(u8 key_index)
{
	printf("pause:%d\n",key_index);
	if(exspi_msc.pause)
	{
		sys_cb.song_mode=key_index;
		exspi_msc.song_index=0;
		exspi_msc.song_int_flag=0;
		exspi_msc.repeat_cnt=0;
	}
	else
	{
		if(exspi_msc.song_int_flag)
		{
			exspi_msc.song_int_flag=0;
			sys_cb.song_mode=key_index;
			exspi_msc.song_index=0;
		}
		else
		{
			
			if(key_index!=sys_cb.song_mode)
			{
				exspi_msc.song_continue_flag=0;
				sys_cb.song_mode=key_index;
				exspi_msc.song_index=0;
				exspi_msc.repeat_cnt=0;
				
			}
			else
			{
				exspi_msc.song_continue_flag=0;
				exspi_msc.song_int_flag=1;
			}
		}

	}
	if(exspi_msc.song_int_flag==0&&sys_cb.project_num)
	{
		exspi_msc.power_on_tts=1;
	}
	set_song_play_num();
}
AT(.text.func.music)
void func_music_QRcode_disable(void)
{
	if(sys_cb.project_num)
	{
		return;
	}
	param_QR_code_disable_read();
	if(sys_cb.QR_code_disable)
	{
		return;
	}
	sys_cb.QR_code_disable=1;
	param_QR_code_disable_write();
	param_sync();
	exspi_msc.QR_disable_tts=1;
	music_control(MUSIC_MSG_STOP);
	set_song_play_num();
}

AT(.text.func.music)
void func_switch_mic_onoff(u8 onoff)
{
	if(onoff)
	{
		if(sys_cb.vbat_low)
		{
			return;
		}
		sys_cb.mic_onoff=1;
		if (get_music_dec_sta() == MUSIC_PLAYING&&(sys_cb.song_mode<2))
		{
		}
		else
		{
			bsp_set_led_mode(LED_SEQUENCE_7);
		}
		if(get_music_dec_sta() != MUSIC_PLAYING)
		{
			amp_speaker_mute();
		}
		bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
		if(get_music_dec_sta() != MUSIC_PLAYING)
		{
			delay_ms(300);
			amp_speaker_unmute();
		}
	}
	else
	{
		sys_cb.mic_onoff=0;
		if(get_music_dec_sta() != MUSIC_PLAYING)
		{
			amp_speaker_mute();
			delay_ms(100);
		}
		bsp_karaok_exit(AUDIO_PATH_KARAOK);
		if (get_music_dec_sta() == MUSIC_PLAYING&&(sys_cb.song_mode==2))
		{
			bsp_set_led_mode(LED_SEQUENCE_5);
		}
		else if(get_music_dec_sta() != MUSIC_PLAYING)
		{
			bsp_set_led_mode(LED_SEQUENCE_2);
		}
	}
}

AT(.text.func.music)
void func_exspifalsh_music_process(void)
{
    func_process();
	func_check_song_play();
	func_song_light_sync_deal();
}

AT(.text.func.music)
bool exspifalsh_music_image_check(void)
{
    if (is_exspiflash_online()) {
        exspi_msc.total_num = exspifalsh_music_get_file_total();
		printf("exspi_msc.total_num = %d\n",exspi_msc.total_num);
        if (exspi_msc.total_num > 0) {
            return true;
        }
    }
    return false;
}

AT(.text.func.music)
void func_check_timeout(void)
{
	if(sys_cb.mic_onoff)
	{
		exspi_msc.timeout_cnt=0;
		return;
	}
	if(exspi_msc.pause)
	{
		exspi_msc.timeout_cnt++;
		printf("music idle cnt:%d\n",exspi_msc.timeout_cnt);
		amp_speaker_mute();
		if(exspi_msc.timeout_cnt>=exspi_msc.max_timeout_cnt)
		{
			exspi_msc.timeout_cnt=0;
			//exspi_msc.song_int_flag=0;
			music_control(MUSIC_MSG_STOP);
			func_cb.sta = FUNC_PWROFF;
		}
	}
}


AT(.text.func.music)
void func_lower_power_check(void)
{
	u8 mic_off;
	if(sys_cb.mic_onoff&&sys_cb.karaok_init)
	 {
	 	
	 	mic_off=bsp_get_mic_vol_offset();
		if(sys_cb.mic_vol_offset!=mic_off)
		{
			sys_cb.mic_vol_offset=mic_off;
			sys_cb.music_vol_offset=mic_off;
			bsp_karaok_set_mic_volume();
			bsp_change_volume(MUSIC_VOL-mic_off);
		}
		if ((sys_cb.vbat <=2700) ||(adc_cb.vrtc_val>=0x39f))
		 {
		 	sys_cb.vbat_low=1;
			sys_cb.mic_onoff=0;
			bsp_karaok_exit(AUDIO_PATH_KARAOK);
		 }
	 }
	else 
	{
		mic_off=bsp_get_mic_vol_offset();
		//printf("vol_offset:%d->offset:%d\n",mic_off,sys_cb.music_vol_offset);
		if(sys_cb.music_vol_offset!=mic_off)
		{
			sys_cb.music_vol_offset=mic_off;
			bsp_change_volume(MUSIC_VOL-mic_off-1);
		}
		
	}
}
AT(.text.func.music)
static void func_exspifalsh_music_enter(void)
{
    msg_queue_clear();
	register_spi_read_function(spiflash1_read);
    if ((!exspifalsh_music_image_check())){
        func_cb.sta = FUNC_NULL;
        return;
    }
    func_cb.mp3_res_play = NULL;//func_exspifalsh_mp3_res_play;
    param_QR_code_disable_read();
    amp_speaker_unmute();
    bsp_change_volume(0);
	#if SYS_KARAOK_EN	
	if(sys_cb.mic_onoff)
	{	
		music_control(MUSIC_MSG_STOP);
		bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
		delay_ms(10);
	}
	#endif   
	bsp_set_volume(MUSIC_VOL);
    exspi_msc.cur_num = 1;            //默认从1首开始播放
    exspi_msc.pause = true;
	exspi_msc.pause_cnt=0;
	exspi_msc.song_index=0;
	exspi_msc.song_int_flag=0;
	printf("song mode:%d\n",sys_cb.song_mode);
	if(sys_cb.song_mode>1)
	{
		sys_cb.song_mode=0;
	}
	exspi_msc.max_timeout_cnt=MAX_TIMEOUT_TIME;
	if(sys_cb.project_num)
	{
		exspi_msc.power_on_tts=1;
	}
	else
	{
		exspi_msc.power_on_tts=0;
	}
	set_song_play_num();
}

AT(.text.func.music)
static void func_exspifalsh_music_exit(void)
{
    music_control(MUSIC_MSG_STOP);
	sys_cb.exspiflash_music_mode=0;
	if(sys_cb.mic_onoff)
	{
		 bsp_karaok_exit(AUDIO_PATH_KARAOK);
		 delay_ms(30);
	}
    register_spi_read_function(NULL);  //恢复内部SPI读接口.
    func_cb.last = FUNC_EXSPIFLASH_MUSIC;
}



AT(.text.func.music)
void func_exspifalsh_music(void)
{
    printf("%s\n", __func__);
	sys_cb.project_num=param_spiflash_project_num_read();
    func_exspifalsh_music_enter();
    while (func_cb.sta == FUNC_EXSPIFLASH_MUSIC) {
        func_exspifalsh_music_process();
        func_exspifalsh_music_message(msg_dequeue());
    }
    func_exspifalsh_music_exit();
}
#endif

